"""
门户网站 - 首页接口
"""
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select

from app.dependencies import get_db
from app.schemas.common import ResponseModel
from app.models.content import Banner, Product, News, ServicePackage, CustomPackage, PackageFeature, FAQ
from app.schemas.content import (
    BannerResponse, ProductResponse, NewsResponse,
    ServicePackageResponse, CustomPackageResponse, PackageFeatureResponse, FAQResponse
)


router = APIRouter()


@router.get("/home", response_model=ResponseModel)
async def get_home_data(db: AsyncSession = Depends(get_db)):
    """
    获取首页数据
    
    包含：
    - Banner轮播图（前5个）
    - 推荐产品（前6个）
    - 最新新闻（前6个）
    - 技术服务套餐（推荐的）
    - 定制开发套餐（热门的）
    - 套餐功能对照（全部）
    - 常见问题（热门的前8个）
    """
    # 获取Banner
    banner_result = await db.execute(
        select(Banner)
        .where(Banner.status == 1)
        .order_by(Banner.sort_order.desc(), Banner.created_at.desc())
        .limit(5)
    )
    banners = banner_result.scalars().all()
    
    # 获取推荐产品
    product_result = await db.execute(
        select(Product)
        .where(Product.status == 1)
        .order_by(Product.sort_order.desc(), Product.created_at.desc())
        .limit(6)
    )
    products = product_result.scalars().all()
    
    # 获取最新新闻
    news_result = await db.execute(
        select(News)
        .where(News.status == 1)
        .order_by(News.is_top.desc(), News.publish_time.desc())
        .limit(6)
    )
    news_list = news_result.scalars().all()
    
    # 获取推荐的技术服务套餐
    service_package_result = await db.execute(
        select(ServicePackage)
        .where(ServicePackage.status == 1)
        .where(ServicePackage.is_recommended == 1)
        .order_by(ServicePackage.sort_order.desc())
        .limit(4)
    )
    service_packages = service_package_result.scalars().all()
    
    # 获取热门定制开发套餐
    custom_package_result = await db.execute(
        select(CustomPackage)
        .where(CustomPackage.status == 1)
        .where(CustomPackage.is_hot == 1)
        .order_by(CustomPackage.sort_order.desc())
        .limit(4)
    )
    custom_packages = custom_package_result.scalars().all()
    
    # 获取套餐功能对照表
    package_feature_result = await db.execute(
        select(PackageFeature)
        .where(PackageFeature.status == 1)
        .order_by(PackageFeature.sort_order.asc())
    )
    package_features = package_feature_result.scalars().all()
    
    # 获取热门FAQ
    faq_result = await db.execute(
        select(FAQ)
        .where(FAQ.status == 1)
        .where(FAQ.is_hot == 1)
        .order_by(FAQ.sort_order.asc())
        .limit(8)
    )
    faqs = faq_result.scalars().all()
    
    return ResponseModel(
        data={
            "banners": [BannerResponse.model_validate(b) for b in banners],
            "products": [ProductResponse.model_validate(p) for p in products],
            "news": [NewsResponse.model_validate(n) for n in news_list],
            "service_packages": [ServicePackageResponse.model_validate(sp) for sp in service_packages],
            "custom_packages": [CustomPackageResponse.model_validate(cp) for cp in custom_packages],
            "package_features": [PackageFeatureResponse.model_validate(pf) for pf in package_features],
            "faqs": [FAQResponse.model_validate(faq) for faq in faqs]
        }
    )

